<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/18
 * Time: 20:56
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\DB;

class Reply
{
    public $reply_id = null;
    public function __construct($reply_id)
    {
        $this->reply_id = $reply_id;
    }

    public static function getReplys($comment_id){
        $replys = DB::table('weibo_comment_replys');
        $result = $replys->where('reply_comment_id','=',$comment_id)->orderBy('reply_like_count','desc')->limit(7)->get();
        if($result){
            $data = [];
            for($i = 0; $i < count($result); $i++){
                $author = new Author($result[$i]->reply_user_id);
                $data[$i] = [
                    'reply' => $result[$i],
                    'author' => $author->replyAuthor()
                ];
            }
            return $data;
        }
        else{
            return null;
        }
    }

    public static function repComment($dataArray){
        $reply = DB::table('weibo_comment_replys');
        $result = $reply->insertGetId([
            'reply_content' => $dataArray['reply_content'],
            'reply_comment_id' => $dataArray['reply_comment_id'],
            'reply_user_id' => $dataArray['reply_user_id']
        ]);
        if($result){
            $reply = $reply->where('reply_id','=',$result)->get();
            if($reply){
                $data = [];
                $author = new Author($reply[0]->reply_user_id);
                $data[0] = [
                    'reply' => $reply[0],
                    'author' => $author->replyAuthor()
                ];
                return $data;
            }
            else{
                return null;
            }
        }
        else{
            return false;
        }
    }

    public function likeReply(){
        $reply = DB::table('weibo_comment_replys');
        $result = $reply->where('reply_id','=',$this->reply_id)->increment('reply_like_count');
        if($result){
            $reply = $reply->where('reply_id','=',$this->reply_id)->select('reply_like_count')->get();
            return $reply[0];
        }
        else{
            return false;
        }
    }

    public static function getReplysAdmin($data){
        $reply = DB::table('weibo_comment_replys');
        $count = $reply->count();
        if(isset($data['orderBy']) && isset($data['order'])){
            if($data['orderBy'] == 'id'){
                if($data['order'] == 'desc'){
                    $reply = $reply -> orderBy('reply_id','desc');
                }
                else{
                    $reply = $reply -> orderBy('reply_id','asc');
                }
            }
            else if($data['orderBy'] == 'like'){
                if($data['order'] == 'desc'){
                    $reply = $reply -> orderBy('reply_like_count','desc');
                }
                else{
                    $reply = $reply -> orderBy('reply_like_count','asc');
                }
            }
            else if($data['orderBy'] == 'time'){
                if($data['order'] == 'desc'){
                    $reply = $reply -> orderBy('reply_datetime','desc');
                }
                else{
                    $reply = $reply -> orderBy('reply_datetime','asc');
                }
            }
            else if($data['orderBy'] == 'comment'){
                if($data['order'] == 'desc'){
                    $reply = $reply -> orderBy('reply_comment_id','desc');
                }
                else{
                    $reply = $reply -> orderBy('reply_comment_id','asc');
                }
            }
            else if($data['orderBy'] == 'author'){
                if($data['order'] == 'desc'){
                    $reply = $reply -> orderBy('reply_user_id','desc');
                }
                else{
                    $reply = $reply -> orderBy('reply_user_id','asc');
                }
            }
            else{
                $reply = $reply -> orderBy('reply_id','asc');
            }
        }
        else{
            $reply = $reply -> orderBy('reply_id','asc');
        }

        if(isset($data['index']) && isset($data['limit'])){
            $reply -> skip(($data['index'] - 1) * $data['limit']) -> take(20);
        }
        else{
            $reply -> skip(1) -> take(20);
        }

        $reply = $reply -> get();

        if($reply){
            $result = [];
            $result['total'] = $count;
            $result['replys'] = $reply;
            return $result;
        }
        else{
            return null;
        }
    }
}